package org.despair2D.ui 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import org.despair2D.debug.Logger;
	import org.despair2D.renderer.IView;
	import org.despair2D.ui.core.ComponentProxy;
	import org.despair2D.ui.puppet.*;
	import org.despair2D.core.INextUpdater;
	import org.despair2D.core.NextUpdaterManager;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
public class Persona extends FusionGroup implements INextUpdater
{	
	
	public function setState( stateData:* ) : void
	{
		if (m_currState && m_outer)
		{
			delete m_personaList[m_stateName]
		}
		
		if (!m_updating)
		{
			NextUpdaterManager.addNextUpdater(this)
			m_updating = true
		}
		
		m_stateName = m_nextState = (stateData != null) ? getQualifiedClassName(stateData) : null
		
		if (m_outer)
		{
			if (m_nextState)
			{
				m_personaList[m_stateName] = this
			}
			else
			{
				Logger.reportError(this, 'setState', '状态更新错误.')
			}
		}
	}
	
	
	final public function modify() : void
	{
		var type:Class = (m_nextState != null) ? getDefinitionByName(m_nextState) as Class : null
		
		m_updating = false
		if (m_currState)
		{
			m_currState.exit()
			m_currState.dispose()
			this.removeAllEventListeners()
			this.removeAllFusions()
			this.removeAllPuppets()
			m_currState = null
		}
		
		// enter
		if (type)
		{
			m_currState = new type()
			m_currState.m_persona = this
			m_currState.preEnter()
			m_currState.enter()
		}
		
		m_nextState = null
	}


	override ns_despair function recycle() : void
	{
		if (m_currState)
		{
			m_currState.exit()
			m_currState.dispose()
			m_currState = null
		}
		
		if (m_outer)
		{
			delete m_personaList[m_stateName]
		}
		
		if (m_updating)
		{
			NextUpdaterManager.removeNextUpdater(this)
			m_updating = false
			m_nextState = null
		}
		
		super.recycle()
	}
	
	
	ns_despair static function createPersona( stateData:*, layer:int, forced:Boolean ) : Persona
	{
		var name:String = getQualifiedClassName(stateData)
		var PA:Persona = m_personaList[name]
		
		if (PA)
		{
			if (forced)
			{
				PA.kill()
			}
			
			else
			{
				Logger.reportWarning('PersonaManager', 'createPersona', '已存在的Persona: ' + name)
				return PA
			}
		}
		
		PA          =  new Persona();
		PA.m_outer  =  true
		PA.setState(stateData)
		
		if (layer == -1)
		{
			m_monitor.addChild(PA.displayObject)
		}
		
		else
		{
			m_monitor.addChildAt(PA.displayObject,m_monitor.numChildren + layer + 1)
		}
		
		return PA
	}
	
	
	/**
	 * 搜索
	 * @param	stateData
	 */
	ns_despair static function retrievePersona( stateData:* ) : Persona
	{
		return m_personaList[getQualifiedClassName(stateData)]
	}
	
	
	
	ns_despair static var m_monitor:Sprite
	
	ns_despair static var m_personaList:Object = new Object()

	
	ns_despair var m_currState:PersonaState
	
	ns_despair var m_nextState:String
	
	ns_despair var m_stateName:String
	
	ns_despair var m_updating:Boolean
	
	ns_despair var m_outer:Boolean
	
	
}

}


	
	//final public function setLayout( code:int, width:Number, height:Number ) : void
	//{
		//var tx:Number, ty:Number
		//
		//switch(code)
		//{
			//case 7:
				//tx = ty = 0
				//break
			//case 8:
				//tx = -width / 2
				//ty = 0
				//break
			//case 9:
				//tx = -width
				//ty = 0
				//break
				//
			//case 4:
				//tx = 0
				//ty = -height / 2
				//break
			//case 5:
				//tx = -width / 2
				//ty = -height / 2
				//break
			//case 6:
				//tx = -width
				//ty = -height / 2
				//break
				//
			//case 1:
				//tx = 0
				//ty = -height
				//break
			//case 2:
				//tx = -width / 2
				//ty = -height
				//break
			//case 3:
				//tx = -width
				//ty = -height
				//break
				//
			//default:
				//Logger.reportError(this, 'modify', '布局代号错误: (' + code + ').')
		//}
		//
		//m_sprite.x = tx
		//m_sprite.y = ty
	//}
	
